package PingPong;

import java.awt.Rectangle;

public class Ball implements Commons {
	public Ball() {
		init();
	}
	
	public void init() {
		x = BOARD_WIDTH / 2;
		y = BOARD_HEIGHT / 2;
		speed = MIN_SPEED;
		angle = 270;
	}
	
	public int getX() {
		return x;
	}
	
	public int getY() {
		return y;
	}
	
	public double getAngle() {
		return angle;
	}
	
	public int getSpeed() {
		return speed;
	}
	
	public boolean getTurn() {
		return rightTurn;
	}
	
	public void setAngle( double a ) {
		angle = Math.abs( a % 360 );
	}
	
	public void setTurn( boolean turn ) {
		rightTurn = turn;
	}
	
	public void setX( int x ) {
		this.x = x;
	}
	
	public void setY( int y ) {
		this.y = y;
	}
	
	public void setSpeed(int s) {
		speed = s;
	}
	
	public void changeSpinning() {
		rightTurn = !rightTurn;
	}
	
	public void captureSpeed( Paddle paddle ) {
		int z;
		
		if( angle <= 90 || angle >= 270 ){
			if( paddle.getDirection() == -1 ) {
				z = 1;
			} else { 
				z = -1;
			}
		} else {
			if( paddle.getDirection() == -1 ) {
				z = -1;
			} else { 
				z = 1;
			}
		}

		speed += paddle.getSpeed()*z;
		if( speed < 0 ) {
			speed = -1*speed;
			rightTurn = !rightTurn;
			System.out.println("zmiana");
		}
		
		if( speed < MIN_SPEED ) {
			speed = MIN_SPEED;
		} else if( speed > MAX_SPEED )
			speed = MAX_SPEED;
	}
	
	public void move( long passed ) {
		double passedTime = passed / 100.0;
		
		if( rightTurn ) {
			angle += speed / 20.0 * passedTime;
		} else {
			angle -= speed / 20.0 * passedTime;
		}
		
		x += speed * Math.sin( Math.toRadians( angle ) ) * passedTime;
		if( x < 0 )
			x = 0;
		else if(  x > BOARD_WIDTH )
			x = BOARD_WIDTH;
		
		y -= speed * Math.cos( Math.toRadians( angle ) ) * passedTime;		
	}
	
	public Rectangle getRectangle() {
		return new Rectangle(x,y,BALL_SIZE,BALL_SIZE);
	}
	
	private int x, y;
	private boolean rightTurn = true;
	private int speed;
	private double angle;
}
